package com.cyms.modules.card.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.cyms.common.annotation.SysLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.card.entity.FuelcardEntity;
import com.cyms.modules.card.entity.FuelcardPayEntity;
import com.cyms.modules.card.service.FuelcardPayService;
import com.cyms.modules.card.service.FuelcardService;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * 油卡消费记录
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-08 14:54:32
 */
@RestController
@RequestMapping("card/fuelcardpay")
public class FuelcardPayController {
    @Autowired
    private FuelcardPayService fuelcardPayService;
    @Autowired
    private FuelcardService fuelcardService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fuelcardPayService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
			FuelcardPayEntity fuelcardPay = fuelcardPayService.selectById(id);

        return R.ok().put("fuelcardPay", fuelcardPay);
    }

    /**
     * 保存
     */
    @SysLog("新增加油卡消费信息")
    @RequestMapping("/save")
    public R save(@RequestBody FuelcardPayEntity fuelcardPay){
    	fuelcardPay.setCreateDate(new Date());
    	fuelcardPay.setRecordDate(new Date());
    	FuelcardEntity fuelcard=fuelcardService.selectById(fuelcardPay.getCardId());
    	if (fuelcard.getRestMoney().compareTo(fuelcardPay.getMoney()) < 0) {
			return R.error("加油卡余额不足");
		}
    	fuelcard.setTotalPay(fuelcard.getTotalPay().add(fuelcardPay.getMoney()));
    	fuelcard.setRestMoney(fuelcard.getRestMoney().subtract(fuelcardPay.getMoney()));
    	
    	fuelcardService.updateById(fuelcard);
    	fuelcardPayService.insert(fuelcardPay);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改加油卡消费信息")
    @RequestMapping("/update")
    public R update(@RequestBody FuelcardPayEntity fuelcardPay){
			fuelcardPayService.updateById(fuelcardPay);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除加油卡消费信息")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
			fuelcardPayService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
